/* $Id: CommonMenu.java 89 2008-01-22 02:38:52Z osborn.steven $
 * 
 * Undroid Package Manager
 *
 * Copyright 2007 Steven Osborn
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */
package com.bitsetters.undroid.common;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.SubMenu;

import com.bitsetters.undroid.CategoryListActivity;
import com.bitsetters.undroid.NewsActivity;
import com.bitsetters.undroid.R;
import com.bitsetters.undroid.RepoListActivity;
import com.bitsetters.undroid.UninstallActivity;
import com.bitsetters.undroid.UpgradeActivity;
import com.bitsetters.undroid.AboutActivity;

/**
 * Menu common to all views
 *
 * @author Steven Osborn - http://steven.bitsetters.com
 */
public class CommonMenu {
    
    public static final String TAG = "UNDROID[CommonMenu]";
    
    public static final int GOTO_GROUP = 0;
    public static final int GOTO = 0;
    public static final int GOTO_INSTALL = 2;
    public static final int GOTO_UNINSTALL = 3;
    public static final int GOTO_UPGRADE = 4;
    public static final int GOTO_REPO = 5;
    public static final int GOTO_NEWS = 6;
    public static final int DEFAULT_GROUP = 1;
    public static final int ABOUT = 100;
    
    public static Activity currentActivity;

    /**
     * 
     * @param context
     * @param menu
     * @return
     */
    public static boolean createMenu(Activity context,Menu menu) {
        currentActivity = context;
        
        SubMenu sub = menu.addSubMenu(CommonMenu.GOTO_GROUP,CommonMenu.GOTO, "Goto");
        sub.add(CommonMenu.GOTO_GROUP, CommonMenu.GOTO_INSTALL, 
                context.getResources().getString(R.string.install))
                .setShortcut(KeyEvent.KEYCODE_1, 0, KeyEvent.KEYCODE_I);
        sub.add(CommonMenu.GOTO_GROUP, CommonMenu.GOTO_UNINSTALL, 
                context.getResources().getString(R.string.uninstall))
                .setShortcut(KeyEvent.KEYCODE_2, 0, KeyEvent.KEYCODE_R);
        sub.add(CommonMenu.GOTO_GROUP, CommonMenu.GOTO_UPGRADE,
                context.getResources().getString(R.string.upgrade))
                .setShortcut(KeyEvent.KEYCODE_3, 0, KeyEvent.KEYCODE_U);
        sub.add(CommonMenu.GOTO_GROUP, CommonMenu.GOTO_REPO,
                context.getResources().getString(R.string.repo))
                .setShortcut(KeyEvent.KEYCODE_4, 0, KeyEvent.KEYCODE_R);
        sub.add(CommonMenu.GOTO_GROUP, CommonMenu.GOTO_NEWS,
                context.getResources().getString(R.string.news))
                .setShortcut(KeyEvent.KEYCODE_5, 0, KeyEvent.KEYCODE_N);
        
        
        menu.addSeparator(CommonMenu.GOTO_GROUP, 0);
        menu.add(CommonMenu.DEFAULT_GROUP,CommonMenu.ABOUT,
                context.getResources().getString(R.string.about_undroid))
                .setShortcut(0, 0, KeyEvent.KEYCODE_A);

        return true;
    }

    /**
     * 
     * @param item
     * @return
     */
    public static boolean handleOption(Menu.Item item) {
        Log.d(TAG,String.valueOf(item.getGroup()));
        Log.d(TAG,String.valueOf(item.getId()));
        Intent i = new Intent();
        if(item.getGroup() == GOTO_GROUP) {
            switch(item.getId()) {
            case GOTO_NEWS:
                i.setClass(CommonMenu.currentActivity, NewsActivity.class);
                CommonMenu.currentActivity.startActivity(i);
                CommonMenu.currentActivity.finish();
                break;
            case GOTO_INSTALL:
                i.setClass(CommonMenu.currentActivity, CategoryListActivity.class);
                CommonMenu.currentActivity.startActivity(i);
                CommonMenu.currentActivity.finish();
                break;
            case GOTO_UNINSTALL:
                i.setClass(CommonMenu.currentActivity, UninstallActivity.class);
                CommonMenu.currentActivity.startActivity(i);
                CommonMenu.currentActivity.finish();
                break;
            case GOTO_REPO:
                i.setClass(CommonMenu.currentActivity, RepoListActivity.class);
                CommonMenu.currentActivity.startActivity(i);
                CommonMenu.currentActivity.finish();
                break;
            case GOTO_UPGRADE:
                i.setClass(CommonMenu.currentActivity, UpgradeActivity.class);
                CommonMenu.currentActivity.startActivity(i);
                CommonMenu.currentActivity.finish();
                break;
            }
        } else if(item.getGroup() == DEFAULT_GROUP) {
            switch(item.getId()) {
            case ABOUT:
                i.setClass(CommonMenu.currentActivity, AboutActivity.class);
                CommonMenu.currentActivity.startActivity(i);
                CommonMenu.currentActivity.finish();
                break;
            }
        }
        return false;
    }
}
